package cn.tedu.order.service.impl;

import cn.tedu.order.entity.Order;
import cn.tedu.order.feign.AccountClient;
import cn.tedu.order.feign.EasyIdClient;
import cn.tedu.order.feign.StorageClient;
import cn.tedu.order.mapper.OrderMapper;
import cn.tedu.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Slf4j
@Service
@GlobalTransactional
/*
*   启动 TM（）
*   连接事务协调器 TC（Transactional Codinator, Seata ）
*   注册全局事务
*   启动 RM（Resource Manager）
* */
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    
    @Autowired
    private AccountClient accountClient;
    
    @Autowired
    private EasyIdClient easyIdClient;
    
    @Autowired
    private StorageClient storageClient;
    
    @Override
    public void create(Order order) {
        //生成一个订单id 用的是（EasyidGenenerator。）
        
        String orderId = easyIdClient.nextId("order_business");
        Long id = Long.parseLong(orderId);
        
        order.setId(id);
        
        orderMapper.create(order);
        log.info("保存订单成功,order = [{}]", order);
      
        storageClient.decrease(order.getProductId(),order.getCount());
        
        accountClient.decrease(order.getUserId(), order.getMoney());
        //TODO 这个todo是一个标签定位，方便找到这个语句
        
    }
}
